package com.zx._03_并发.chapter_08_threadLocal;

import java.lang.reflect.Field;
import org.springframework.util.ReflectionUtils;

public class ThreadLocalDemo extends Thread {

    // ThreadLocal 是当前线程的私有属性，所有不涉及到线程安全的问题
    // local1 和 local1，可以认为是帮助我们调用线程中的这个私有属性,在简单说，ThreadLocal只是个工具类
    // ThreadLocal#ThreadLocal.ThreadLocalMap threadLocals
    private static ThreadLocal<String> local1 = new ThreadLocal<String>();

    private static ThreadLocal<String> local2 = new ThreadLocal<String>();

    public static void main(String[] args) throws Exception {
        Thread currentThread = Thread.currentThread();
        local1.set("1");
        local2.set("2");

        System.out.println(local1.get());
        System.out.println(local2.get());

        Field field = ReflectionUtils.findField(Thread.class, "threadLocals");
        field.setAccessible(true);

        Object object = field.get(currentThread);
        System.out.println(object);
    }

}
